set more off
clear all
version 17.0

set matsize 10000


global xlist linccap lsize housing_nrooms lagehead lyeduc cook_jobweeks lyeduccook housing_selfowned sexhead agrihead unemphead cook_female d_road elec rural i.religion i.ethnicgroup //i.location
global xlisturban linccap lsize housing_nrooms lagehead lyeduc cook_jobweeks lyeduccook housing_selfowned sexhead agrihead unemphead cook_female i.religion i.ethnicgroup //i.location
global reglist linccap lsize housing_nrooms lagehead lyeduc cook_jobweeks 
global absorblist housing_selfowned sexhead agrihead unemphead cook_female d_road elec location religion ethnicgroup 
global absorblisturban housing_selfowned sexhead agrihead unemphead cook_female location religion ethnicgroup  
global absorblistDID housing_selfowned sexhead agrihead unemphead cook_female d_road elec location religion ethnicgroup
global absorblisturbanDID housing_selfowned sexhead agrihead unemphead cook_female location religion ethnicgroup



************************************************************************************************
*** Entropy balancing weights *********************************************************************************
************************************************************************************************

*************
*** DiD ************************************************************************
use "$datain\glss_DiDsample.dta", clear
cd "$output"
keep if (glss==6 & (aftercategory==2 | aftercategory==3)) | (glss==5 & eligible5==1)
keep if cooking!=0
drop if cooking==.

merge 1:1 hid using "$output\weights_eb"
drop _merge

foreach est in ate cater cateu {
	cap noisily replace weight_`est' = weight_`est'5 if glss==5
	cap noisily replace weight_`est'_coal = weight_`est'5_coal if glss==5
	cap noisily replace weight_`est'_lpg = weight_`est'5_lpg if glss==5
}

//ATT
reghdfe d_wood treat did $reglist [aweight=weight_ate], absorb($absorblistDID ) vce(cluster clustervar)
est store main1 
reghdfe d_charcoal treat did $reglist [aweight=weight_ate], absorb($absorblistDID ) vce(cluster clustervar)
est store main2
reghdfe d_lpg treat did $reglist [aweight=weight_ate], absorb($absorblistDID ) vce(cluster clustervar)
est store main3

reghdfe lq_coal_hat treat did $reglist [aweight=weight_ate], absorb($absorblistDID ) vce(cluster clustervar)
est store coal1
reghdfe d_qcoal treat did $reglist [aweight=weight_ate], absorb($absorblistDID ) vce(cluster clustervar)
est store coal2
reghdfe lq_coal_hat2 treat did $reglist [aweight=weight_ate_coal], absorb($absorblistDID ) vce(cluster clustervar)
est store coal3

reghdfe lexp_lpg treat did $reglist [aweight=weight_ate], absorb($absorblistDID ) vce(cluster clustervar)
est store lpg1
reghdfe d_elpg treat did $reglist [aweight=weight_ate], absorb($absorblistDID ) vce(cluster clustervar)
est store lpg2
reghdfe lexp_lpg2 treat did $reglist [aweight=weight_ate_lpg], absorb($absorblistDID ) vce(cluster clustervar)
est store lpg3

//Rural
gen didr=did
reghdfe d_wood treat didr $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store main1r
reghdfe d_charcoal treat didr $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store main2r
reghdfe d_lpg treat didr $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store main3r

reghdfe lq_coal_hat treat didr $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store coal1r
reghdfe d_qcoal treat didr $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store coal2r

reghdfe lexp_lpg treat didr $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store lpg1r
reghdfe d_elpg treat didr $reglist [aweight=weight_cater] if rural==1, absorb($absorblistDID ) vce(cluster clustervar)
est store lpg2r

//Urban
gen didu=did
reghdfe d_wood treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store main1u
reghdfe d_charcoal treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store main2u
reghdfe d_lpg treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store main3u

reghdfe lq_coal_hat treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store coal1u
reghdfe d_qcoal treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store coal2u
reghdfe lq_coal_hat2 treat didu $reglist [aweight=weight_cateu_coal] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store coal3u

reghdfe lexp_lpg treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store lpg1u
reghdfe d_elpg treat didu $reglist [aweight=weight_cateu] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store lpg2u
reghdfe lexp_lpg2 treat didu $reglist [aweight=weight_cateu_lpg] if rural==0, absorb($absorblisturbanDID ) vce(cluster clustervar)
est store lpg3u


//Figure 4
coefplot (main1,offset(-.2) mlabel label(Firewood) msymbol(O) mcolor(gs5) ciopts(recast(rcap) lcolor(gs5) lwidth(thin))) ///
	(main2,offset(0) label(Charcoal) msymbol(D) mcolor(gs8) ciopts(recast(rcap) lcolor(gs5) lwidth(thin))) ///
	(main3 ,offset(.2) label(LPG) msymbol(T) mcolor(gs11) graphregion(color(white)) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(7)) ///
	,graphregion(color(white)) yline(0, lcolor(gs12)) name(ate1, replace) legend(region(lcolor(white)) rows(1)) ///
	 title("A: Main fuel choice", color(black)) mlabposition(3) mlabcolor(gs0) mlabsize(medsmall) msize(medlarge) format(%9.3f) vertical ylab(,nogrid) xlabel(none) keep(did)

coefplot (coal1 ,  offset(-.2) label("Average") msymbol(S) mcolor(gs8) ciopts(recast(rcap) lcolor(gs5) lwidth(thin))) ///
	(coal2 , offset(0) label("Extensive") msymbol(X) msize(large) mcolor(gs8) ciopts(recast(rcap) lcolor(gs5) lwidth(thin))) ///
	(coal3, offset(.2) label("Intensive") msymbol(T) mcolor(gs8) ciopts(recast(rcap) lcolor(gs5) lwidth(thin))) ///
	,graphregion(color(white)) yline(0, lcolor(gs12)) name(ate2, replace) legend(region(lcolor(white)) rows(1)) ///
	 format(%9.3f) mlabposition(9) mlabcolor(gs0) mlabsize(medsmall) msize(medlarge) ///
	 title("B: Charcoal consumption", color(black))  vertical ylab(,nogrid)  xlabel(none) keep(did)

coefplot (lpg1, offset(-.2) label("Average") msymbol(S) mcolor(gs11) ciopts(recast(rcap) lcolor(gs5) lwidth(thin))) ///
	(lpg2 , offset(0) label("Extensive") msymbol(X) msize(large) mcolor(gs11) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(9)) ///
	(lpg3 , offset(.2) label("Intensive") msymbol(T) mcolor(gs11) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(9)) ///
	,graphregion(color(white)) yline(0, lcolor(gs12)) name(ate3, replace) legend(region(lcolor(white)) rows(1)) ///
	 format(%9.3f) mlabposition(1) mlabcolor(gs0) mlabsize(medsmall) msize(medlarge) ///
	 title("C: LPG expenditure", color(black))  vertical ylab(,nogrid)  xlabel(none) keep(did)

graph combine ate1 ate2 ate3, rows(1) graphregion(color(white)) 
graph display, xsize(8)
graph export "$output\fig4_att.pdf", replace


//Figure 5
coefplot (main1r ,offset(-.2) label(Firewood) mlabel msymbol(O) mcolor(gs5) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(3)) ///
	(main1u ,offset(-.2) label(Firewood) mlabel msymbol(O) mcolor(gs5) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(9)) ///
	(main2r ,offset(0) label(Charcoal) msymbol(D) mcolor(gs8) ciopts(recast(rcap) lcolor(gs5) lwidth(thin) mlabposition(9))) ///
	(main2u ,offset(0) mlabel label(Charcoal) msymbol(D) mcolor(gs8) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) ) ///
	(main3r \ main3u ,offset(.2) label(LPG) msymbol(T) mcolor(gs11) graphregion(color(white)) ciopts(recast(rcap) lcolor(gs5) lwidth(thin))) ///
	,graphregion(color(white)) yline(0, lcolor(gs12)) name(cate1, replace) legend(region(lcolor(white)) order(2 8 10) rows(1)) ///
	 title("A: Main fuel choice", color(black)) mlabcolor(gs0) mlabsize(medsmall) msize(medlarge) format(%9.3f) vertical ylab(,nogrid) ///
	 coeflabels(didr="Rural" didu="Urban") xline(1.5, lcolor(gs15)) keep(didr didu) mlabposition(9)
  
coefplot (coal1r, offset(-.2) label("Average") msymbol(S) mcolor(gs8) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(3)) ///
	(coal1u, offset(-.2) label("Average") mlabel msymbol(S) mcolor(gs8) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(3)) ///
	(coal2r, offset(0) label("Extensive") msymbol(X) mcolor(gs8) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(3)) ///
	(coal2u, offset(0) label("Extensive") msymbol(X) mcolor(gs8) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(3)) ///
	///(coal3r, offset(.2) label("Combined") mlabel msymbol(T) mcolor(gs8) ciopts(recast(rcap) lcolor(gs5) lwidth(thin))) ///
	(coal3u, offset(.2) label("Intensive") mlabel msymbol(T) mcolor(gs8) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(3)) ///
	,graphregion(color(white)) yline(0, lcolor(gs12)) name(cate2, replace) legend(region(lcolor(white)) order(2 6 10) rows(1)) ///
	 format(%9.3f) mlabcolor(gs0) mlabsize(medsmall) msize(medlarge) ///
	 title("B: Charcoal consumption", color(black))  vertical ylab(,nogrid) ///
	 coeflabels(didr="Rural" didu="Urban") xline(1.5, lcolor(gs15))  keep(didr didu)
	 
coefplot (lpg1r, offset(-.2) label("Average") ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) msymbol(S) mcolor(gs11)) ///
	(lpg1u, offset(-.2) label("Average") msymbol(S) mcolor(gs11) ciopts(recast(rcap) lcolor(gs5) lwidth(thin))) ///
	(lpg2r, offset(0) label("Extensive") msymbol(X) mcolor(gs11) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(8)) ///
	(lpg2u, offset(0) label("Extensive") msymbol(X) mcolor(gs11) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(9)) ///
	///(lpg3r, offset(.2) label("Intensive") mlabel msymbol(T) mcolor(navy) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(1)) ///
	(lpg3u, offset(.2) label("Intensive") msymbol(T) mcolor(gs11) ciopts(recast(rcap) lcolor(gs5) lwidth(thin)) mlabposition(9)) ///
	,graphregion(color(white)) yline(0, lcolor(gs12)) name(cate3, replace) legend(region(lcolor(white)) order(2 6 10) rows(1)) ///
	 format(%9.3f) mlabposition(1) mlabcolor(gs0) mlabsize(medsmall) msize(medlarge) ///
	 title("C: LPG expenditure", color(black))  vertical ylab(,nogrid) ///
	 coeflabels(didr="Rural" didu="Urban") xline(1.5, lcolor(gs15)) keep(didr didu)

graph combine cate1 cate2 cate3, rows(1) graphregion(color(white))
graph display, xsize(8)
graph export "$output\fig5_catt.pdf", replace

//Table A8
local filename table_a8_did_att
est restore main1
outreg2 using "$output\\`filename'.tex", replace tex(fragment) label dec(3) adjr2
est restore main2
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore main3
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal1
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal2
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal3
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg1
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg2
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg3
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2

local filename table_a8_did_cattr
est restore main1r
outreg2 using "$output\\`filename'.tex", replace tex(fragment) label dec(3) adjr2
est restore main2r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore main3r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal1r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal2r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg1r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg2r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2

local filename table_a8_did_cattu
est restore main1u
outreg2 using "$output\\`filename'.tex", replace tex(fragment) label dec(3) adjr2
est restore main2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore main3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal1u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg1u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2



************************************************************************************************
*** CBPS weights *********************************************************************************
************************************************************************************************

*************
*** DiD **********************************************************************
use "$datain\glss_DiDsample.dta", clear
cd "$output"
keep if (glss==6 & (aftercategory==2 | aftercategory==3)) | (glss==5 & eligible5==1)
keep if cooking!=0
drop if cooking==.

merge 1:1 hid using "$output\weights_cbps"
drop _merge


*** full sample ***
reghdfe d_wood treat did $reglist [aweight=weight_ate1], absorb($absorblist ) vce(cluster clustervar)
est store main1
reghdfe d_charcoal treat did $reglist [aweight=weight_ate1], absorb($absorblist ) vce(cluster clustervar)
est store main2
reghdfe d_lpg treat did $reglist [aweight=weight_ate1], absorb($absorblist ) vce(cluster clustervar)
est store main3

reghdfe lq_coal_hat treat did $reglist [aweight=weight_ate1], absorb($absorblist ) vce(cluster clustervar)
est store coal1
reghdfe d_qcoal treat did $reglist [aweight=weight_ate1], absorb($absorblist ) vce(cluster clustervar)
est store coal2
reghdfe lq_coal_hat2 treat did $reglist [aweight=weight_ate2], absorb($absorblist ) vce(cluster clustervar)
est store coal3

reghdfe lexp_lpg treat did $reglist [aweight=weight_ate1], absorb($absorblist ) vce(cluster clustervar)
est store lpg1
reghdfe d_elpg treat did $reglist [aweight=weight_ate1], absorb($absorblist ) vce(cluster clustervar)
est store lpg2
reghdfe lexp_lpg2 treat did $reglist [aweight=weight_ate3], absorb($absorblist ) vce(cluster clustervar)
est store lpg3

*** rural ***
reghdfe d_wood treat did $reglist [aweight=weight_cater1], absorb($absorblist ) vce(cluster clustervar)
est store main1r
reghdfe d_charcoal treat did $reglist [aweight=weight_cater1], absorb($absorblist ) vce(cluster clustervar)
est store main2r
reghdfe d_lpg treat did $reglist [aweight=weight_cater1], absorb($absorblist ) vce(cluster clustervar)
est store main3r

reghdfe lq_coal_hat treat did $reglist [aweight=weight_cater1], absorb($absorblist ) vce(cluster clustervar)
est store coal1r
reghdfe d_qcoal treat did $reglist [aweight=weight_cater1], absorb($absorblist ) vce(cluster clustervar)
est store coal2r
*reghdfe lq_coal_hat2 treat $reglist [aweight=weight_cater2], absorb($absorblist ) vce(cluster clustervar)
*est store coal3

reghdfe lexp_lpg treat did $reglist [aweight=weight_cater1], absorb($absorblist ) vce(cluster clustervar)
est store lpg1r
reghdfe d_elpg treat did $reglist [aweight=weight_cater1], absorb($absorblist ) vce(cluster clustervar)
est store lpg2r
*reghdfe lexp_lpg2 treat $reglist [aweight=weight_cater3], absorb($absorblist ) vce(cluster clustervar)
*est store lpg3


*** urban ***
reghdfe d_wood treat did $reglist [aweight=weight_cateu1], absorb($absorblisturban ) vce(cluster clustervar)
est store main1u
reghdfe d_charcoal treat did $reglist [aweight=weight_cateu1], absorb($absorblisturban ) vce(cluster clustervar)
est store main2u
reghdfe d_lpg treat did $reglist [aweight=weight_cateu1], absorb($absorblisturban ) vce(cluster clustervar)
est store main3u

reghdfe lq_coal_hat treat did $reglist [aweight=weight_cateu1], absorb($absorblisturban ) vce(cluster clustervar)
est store coal1u
reghdfe d_qcoal treat did $reglist [aweight=weight_cateu1], absorb($absorblisturban ) vce(cluster clustervar)
est store coal2u
reghdfe lq_coal_hat2 treat did $reglist [aweight=weight_cateu2], absorb($absorblisturban ) vce(cluster clustervar)
est store coal3u

reghdfe lexp_lpg treat did $reglist [aweight=weight_cateu1], absorb($absorblisturban ) vce(cluster clustervar)
est store lpg1u
reghdfe d_elpg treat did $reglist [aweight=weight_cateu1], absorb($absorblisturban ) vce(cluster clustervar)
est store lpg2u
reghdfe lexp_lpg2 treat did $reglist [aweight=weight_cateu3], absorb($absorblisturban ) vce(cluster clustervar)
est store lpg3u

*** Table A5
local filename table_a5_did_cbps_att
est restore main1
outreg2 using "$output\\`filename'.tex", replace tex(fragment) label dec(3) adjr2
est restore main2
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore main3
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal1
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal2
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal3
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg1
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg2
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg3
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2

local filename table_a5_did_cbps_cattr
est restore main1r
outreg2 using "$output\\`filename'.tex", replace tex(fragment) label dec(3) adjr2
est restore main2r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore main3r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal1r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal2r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg1r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg2r
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2

local filename table_a5_did_cbps_cattu
est restore main1u
outreg2 using "$output\\`filename'.tex", replace tex(fragment) label dec(3) adjr2
est restore main2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore main3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal1u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore coal3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg1u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg2u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2
est restore lpg3u
outreg2 using "$output\\`filename'.tex", append tex(fragment) label dec(3) adjr2


exit

